<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <tab-control class="tab-control" :titles="['潮流','新款','精选']"
                 @tabClick="tabClick" ref="tabControl1" v-show="isTabFixed"/>
    <scroll class="content" ref="scroll" :probe-type="3"
            @scroll="contentScroll" :pull-up-load="true"
            @pullingUp="loadMore">
        <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"></home-swiper>
        <recommend-view :recommends="recommends"/>
        <feature-view/>
        <tab-control :titles="['潮流','新款','精选']"
                     @tabClick="tabClick" ref="tabControl2"/>
        <goods-list :goods="showGoods"/>
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import TabControl from "@/components/content/tabControl/TabControl";
import HomeSwiper from "views/home/childComps/HomeSwiper";
import RecommendView from "views/home/childComps/RecommendView";
import FeatureView from "@/views/home/childComps/FeatureView";
import GoodsList from "@/components/content/goods/GoodsList";
import Scroll from "@/components/common/scroll/Scroll";
import BackTop from "@/components/common/backtop/BackTop";

import {debounce} from "@/common/utils";
import {getHomeMultidata,getHomeGoods} from "network/home";


export default {
  name: "Home",
  components: {
    NavBar,
    TabControl,
    HomeSwiper,
    RecommendView,
    FeatureView,
    GoodsList,
    BackTop,
    Scroll
  },
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        'pop': {page: 0,list: []},
        'new': {page: 0,list: []},
        'sell': {page: 0,list: []},
      },
      currentType: 'pop',
      isShowBackTop: false,
      tabOffSetTop: 0,
      isTabFixed: false
    }
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list
    }
  },
  created() {
    // 1.请求多个数据
    this.getHomeMultidata()
    // 2.请求商品数据
    this.getHomeGoods('pop')
    this.getHomeGoods('new')
    this.getHomeGoods('sell')

  },
  activated() {
    this.$refs.scroll.scrollTo(0,this.saveY,0)
    this.$refs.scroll.refresh()
  },
  deactivated() {
    this.saveY = this.$refs.scroll.getScrollY()
  },
  mounted() {
    // 1.监听item中的图片加载完成
    const refresh = debounce(this.$refs.scroll.refresh,200)
    this.$bus.$on('itemImageLoad',() => {
      // 刷新
      refresh()
    })

  },
  methods: {
    /**
     *  事件监听
     */
    swiperImageLoad(){
      this.tabOffSetTop = this.$refs.tabControl2.$el.offsetTop
    },
    tabClick(index){
      switch (index){
        case 0:
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    backClick(){
      // console.log('点击事件');
      this.$refs.scroll.scrollTo(0,0)
    },
    contentScroll(position){
      // console.log(position);
      // 1.决定backtop是否显示
      this.isShowBackTop = (-position.y) > 1000
      // 2.决定tabcontrol是否吸顶
      this.isTabFixed = (-position.y) > this.tabOffSetTop
    },
    loadMore(){
      this.getHomeGoods(this.currentType)
    },
    /**
     *  网络请求
     */
    getHomeMultidata(){
      getHomeMultidata().then(res => {
        // console.log(res)
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      })
    },
    getHomeGoods(type){
      const page = this.goods[type].page + 1
      getHomeGoods(type,page).then(res => {
        // ...自动解析把数组内容挨个放进变量里面
        this.goods[type].list.push(...res.data.list)
        // 页码+1
        this.goods[type].page += 1
        // console.log(res);
        // 完成上拉加载更多
        this.$refs.scroll.finishPullUp()
      })
    }
  }
}
</script>

<style scoped>
   #home{
     width: 100%;
     height: 100vh;
     /*padding-top: 44px;*/
     position: relative;
  }
  .home-nav{
    background-color: #ff8198;
    color: #f6f6f6;

    /*position: fixed;*/
    /*left: 0;*/
    /*right: 0;*/
    /*top: 0;*/
    /*z-index: 9;*/
  }
   .tab-control{
     background-color: #f6f6f6;
     position: relative;
     z-index: 9;
   }
  /*.tab-control{*/
  /*  background-color: #f6f6f6;*/
  /*  !*在没有达到44px的时候是static*/
  /*    达到的时候变成fixed*/
  /*    吸顶效果*/
  /*  *!*/
  /*  position: sticky;*/
  /*  top: 44px;*/
  /*  z-index: 9;*/
  /*}*/

  .content{
    overflow: hidden;

    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }
</style>
